<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class supplier_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		
	public function insert_supplier($data)
	{
		$this->db->insert('supplier',$data);
		$latest_id = $this->db->insert_id();
		return $latest_id;
	}
	
	
	public function insert_account($data){
	if($data['type']=='Credit'){
		$data1=array(
			'id_user' => $data['id_user'],
			'user_name' => $data['name'],
			'voucher_type' => 'Opening Balance',
			'payment_type' => 5,
			'payment_mode' => 'Opening Balance',
			'voucher_no' => 'Opening Balance',
			'date' => date('Y-m-d'),
			'debit_amount' => $data['opening_balance'],
			'ip' => $_SERVER['REMOTE_ADDR']
		);
		$this->db->insert('accounts',$data1);
				
	}	else{
		
			$data1=array(
			'id_user' => $data['id_user'],
			'user_name' => $data['name'],
			'voucher_type' => 'Opening Balance',
			'payment_type' => 5,
			'payment_mode' => 'Opening Balance',
			'voucher_no' => 'Opening Balance',
			'date' => date('Y-m-d'),
			'credit_amount' => $data['opening_balance'],
			'ip' => $_SERVER['REMOTE_ADDR']
		);
		$this->db->insert('accounts',$data1);
		
	}
		
}

public function update_account($data){
	if($data['type']=='Credit'){
		$data1=array(
			'id_user' => $data['id_user'],
			'user_name' => $data['name'],
			'voucher_type' => 'Opening Balance',
			'payment_type' => 5,
			'payment_mode' => 'Opening Balance',
			'voucher_no' => 'Opening Balance',
			'date' => date('Y-m-d'),
			'credit_amount' => 0,
			'debit_amount' => $data['opening_balance'],
			'ip' => $_SERVER['REMOTE_ADDR']
		);
		
		$fetch_data = $this->db->get_where('accounts',array('id_user'=> $data['id_user'],'payment_type' => 5 ));
		if($fetch_data->num_rows() > 0){
			$this->db->update('accounts',$data1,array('id_user'=> $data['id_user'],'payment_type' => 5 ));
		}else{
			$this->db->insert('accounts',$data1);
		}
				
	}	else{
		
			$data1=array(
			'id_user' => $data['id_user'],
			'user_name' => $data['name'],
			'voucher_type' => 'Opening Balance',
			'payment_type' => 5,
			'payment_mode' => 'Opening Balance',
			'voucher_no' => 'Opening Balance',
			'date' => date('Y-m-d'),
			'debit_amount' => 0,
			'credit_amount' => $data['opening_balance'],
			'ip' => $_SERVER['REMOTE_ADDR']
		);
		
		$fetch_data = $this->db->get_where('accounts',array('id_user'=> $data['id_user'],'payment_type' => 5 ));
		if($fetch_data->num_rows() > 0){
			$this->db->update('accounts',$data1,array('id_user'=> $data['id_user'],'payment_type' => 5 ));
		}else{
			$this->db->insert('accounts',$data1);
		}
		
	}
				
}
	
	
	
	
	
	
	
	public function create_department($insert)
	{
		$this->db->insert('department',$insert);
	}
	
	public function add_group($name,$parent,$additional_data){
		
		$data = array(
			    'name'   => $name,
				'parent'   => $parent
		 );
		// print_r($data);exit;
		$this->db->insert('acc_group',$data);
	}
	
	public function get_category(){
			$q = $this->db->get('category');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}
	
	public function get_company(){
		$q = $this->db->get('company');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
			return $data;
		}
		
	}
	
	
		
	public function get_branch(){
	$q = $this->db->get('branch');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}
	

	public function get_supplierList(){
			//$q = $this->db->get('users')->result();
			//print_r($q);exit;
	$this->db->select('supplier.*');
	$this->db->from('users', 'users_groups' );
	$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 4  '  );
	//$this->db->join('company', 'product.id_company= company.id');
	$result = $this->db->get();
	return $result;
		//echo "i am here";exit;
/*		
		$this->db->select('p.*');
		$this->db->from('product p, category cg, company cm');
	//	$this->db->where('product.group ='.$id);
		$this->db->join('category cg','p.id_category=cg.id');
		$this->db->join('company cm','p.id_company=cm.id');
		 $query = $this->db->get();
		echo "i am here";
		print_r($query->result());exit;*/
	
	
		//$query = $this->db->get('product');
	//eturn $query;

		
		
		} 

public function get_product($id){
	$query = $this->db->get_where('product', array('id' => $id));
	return $query;

	}



/*public function get_groups(){
	$this->db->select('acc_group.*,projects.name');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();
		$query = $this->db->get('acc_group');
	return $query;
}
*/
public function edit_supplier($id){
		$query = $this->db->get_where('supplier', array('id' => $id));
		return $query;
	}

public function update($edit_id, $data){
		$this->db->update('supplier',$data,array('id'=>$edit_id));
	}

public function delete($id){
		$this->db->delete('supplier',array('id'=>$id));
	}
	
public function get(){
	   $str = $this->datatables
			->select("supplier.id as id, supplier.name, supplier.contact_person, supplier.tin_no, supplier.phone,supplier.mobile, supplier.email, supplier.opening_balance,supplier.address")
			->from("supplier")
			->add_column("Actions", 
			"<center>			<a href='edit_supplier/$1' title='Edit Supplier'>
								<i class='icon-edit'></i>
								</a>&nbsp;&nbsp;
							    <a href='delete_supplier/$1' title='Delete Supplier' onClick=\"return confirm('". 'Do U Want to Delete supplier' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")
			->unset_column('id');
		
	   return  $this->datatables->generate();	
}	



}
